package com.bw.lyc.mvp.view.people;

import android.os.Bundle;
import android.view.View;

import com.bw.lyc.R;
import com.bw.lyc.mvp.base.model.BaseEntity;
import com.bw.lyc.mvp.base.view.BaseFragment;
import com.bw.lyc.mvp.contract.LREContract;
import com.bw.lyc.mvp.di.DaggerLREComponent;
import com.bw.lyc.mvp.di.LREModules;
import com.bw.lyc.mvp.model.HttpCode;
import com.bw.lyc.mvp.model.entity.LiveEntity;
import com.bw.lyc.mvp.model.entity.NewsEntity;
import com.bw.lyc.mvp.model.entity.RecommendEntity;
import com.bw.lyc.mvp.presenter.LREPresenter;
import com.bw.lyc.mvp.view.live.LiveValuesActivity;
import com.bw.lyc.mvp.view.news.NewsValuesActivity;
import com.bw.lyc.mvp.view.people.adapter.RecommendAdapter;
import com.bw.lyc.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RecommendFragment extends BaseFragment<LREPresenter> implements
        LREContract.ILREView, OnItemChildClickListener, OnItemClickListener {

    private RecyclerView recommend_rv;
    private LinearLayoutManager manager;
    private RecommendAdapter adapter;

    @Override
    public int bindLayout() {
        return R.layout.fragment_recommend;
    }

    @Override
    public void initView() {
        recommend_rv = f(R.id.recommend_rv);
        manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recommend_rv.setLayoutManager(manager);
        adapter = new RecommendAdapter();
        adapter.setOnItemChildClickListener(this);
        recommend_rv.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", HttpCode.RECOMMENDNEWSCODE);
        p.requestAll(map);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(
                new LREModules(this)).build().injectRecommendFragment(this);
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof RecommendEntity) {
            RecommendEntity recommendEntity = (RecommendEntity) entity;
            adapter.setNewInstance(recommendEntity.getValues());
        }
    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {

    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        List<RecommendEntity.Values> list = (List<RecommendEntity.Values>) adapter.getData();
        ToastUtils.showMsg("当前点击:" + list.get(position).news_title);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        RecommendEntity.Values entity = (RecommendEntity.Values) adapter.getItem(position);
        Bundle bundle = new Bundle();
        bundle.putLong("news_id", entity.news_id);
        bundle.putString("news_url", entity.news_url);
        startPage(bundle, NewsValuesActivity.class);
    }
}
